home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / util / dir / Drawersize.lha / Drawersize / Drawersize.bb2 < prev    next >
Text File  |  1999-01-17  |  9KB  |  336 lines

  1. ;*******************************************************************
  2. ;                                                                  *
  3. ;DrawerSize.bb2                                                    *
  4. ;                                                                  *
  5. ;Program to attach a menu item to WB Tools menu which finds the    *
  6. ;size of a drawer, its subdrawers and files and displays           *
  7. ;no. of bytes on WBScreen title bar. Runs from WBStartup drawer.   *
  8. ;                                                                  *
  9. ;Written in Blitz Basic II                                         *
  10. ;                                                                  *
  11. ;Fully working prototype! 5/10/94                                  *
  12. ;Finished (?) by HVB, SPR 25/10/94                                 *
  13. ;(Made compatible with WB prefs 26/2/95) not relevant to this ver. *
  14. ;Cured a particularly elusive and fatal bug 17/3/95                *
  15. ;Made into a commodity 17/4/95                                     *
  16. ;                                                                  *
  17. ;Replaced custom titlebar output with ShowTitle_ (again!) 28/3/97  *
  18. ;                                                                  *
  19. ;amigalibs.res resident using Compiler Options                     *
  20. ; (must be the LATEST, ie 3.0!!!)                                  *
  21. ;                                                                  *
  22. ;*******************************************************************
  23.  
  24. ;system structure defs. Thank God for amigalibs.res!
  25.  
  26. DEFTYPE.MsgPort *wb_port, *cx_port
  27. DEFTYPE.AppMessage *appmsg
  28. DEFTYPE.FileInfoBlock *fib
  29. DEFTYPE.Screen *wbscreen
  30. DEFTYPE.NewBroker newbroker
  31. DEFTYPE.l appitem, cx_msgid, *broker
  32.  
  33. title$ = "Select Drawer icon(s) for size in bytes." ;initial message
  34. user_data$ = "C:Command"
  35. amItem$ = "Drawer Size"
  36. cx_name$ = "DrawerSize"
  37. cx_title$ = "DrawerSize (C) H Brown, S Rouse 1995"
  38. cx_descr$ = "Finds size of drawer & files in bytes."
  39. wbscreenname$ = "Workbench"
  40. NUL.l = 0
  41. total.l = 0
  42. quit_prog = False
  43. enabled = True
  44.  
  45.  
  46. ;*****************************************************************
  47. ;
  48. ;Functions
  49. ;
  50. ;*****************************************************************
  51.  
  52. ;-----------------------------------------------------------------
  53. ; GetSize{path$}  ;idea pinched from WhereIs by Paul Andrews
  54. ;
  55. ; This is a recursive statement. Work it out for yourselves!
  56. ;-----------------------------------------------------------------
  57.  
  58. Statement GetSize{path$}
  59. SHARED total,*fib   ;can't use any variables from the main program
  60.                     ;unless they are SHAREd
  61.  
  62.   lock.l=Lock_(&path$,-2)
  63.   gotFib.l=Examine_(lock,*fib)
  64.  
  65.   If gotFib=0 Then Goto failed
  66.  
  67.   While ExNext_(lock,*fib)<>0
  68.  
  69.     ;filename is stored as a char string fib_FileName
  70.     ;WITHIN the fib structure (max 108 chars)
  71.     ;&*fib\fib_FileName gets the address of the first
  72.     ;char in the string. Peek$(&) loads n$ with the
  73.     ;null terminated string starting at this address
  74.     n$ = Peek$(&*fib\fib_FileName)
  75.     n$ = LCase$(n$)
  76.  
  77.     filetype.l = *fib\fib_DirEntryType
  78.  
  79.     If filetype<0          ;file is not a drawer
  80.  
  81.       total = total + *fib\fib_Size
  82.  
  83.     EndIf
  84.  
  85.     If filetype=>0  ;file is a drawer
  86.  
  87.       p$=path$
  88.       If Right$(p$,1) = ":" OR Right$(p$,1) = "/"
  89.         p$=p$ + n$
  90.       Else
  91.         p$=p$ + "/" + n$
  92.       EndIf
  93.       GetSize{p$}  ;HELP!!!!!!
  94.  
  95.     EndIf
  96.  
  97. Wend
  98.  
  99. failed:
  100.  
  101.   UnLock_ lock
  102.  
  103. End Statement
  104.  
  105. ;**************************************************************
  106. ;
  107. ;  Program Entry
  108. ;
  109. ;**************************************************************
  110.  
  111. WBStartup
  112.  
  113. If ExecVersion < 37 Then End  ;SORRY, CHAPS!
  114.  
  115. ;allocate the space for a file info block
  116. *fib=AllocMem_(SizeOf.FileInfoBlock,#MEMF_PUBLIC|#MEMF_CLEAR)
  117.  
  118. Format "###,###,###,###"
  119.  
  120. ;------------------------------------------------------------------
  121. ;Create a message port so that WB can signal when our menu selected
  122. ;------------------------------------------------------------------
  123.  
  124. *wb_port = CreateMsgPort_()
  125. wb_PortSig.l = 1 LSL *wb_port\mp_SigBit
  126.  
  127. ;-------------------------------------------------------------------
  128. ;Attach our menu item to WB Tools menu
  129. ;-------------------------------------------------------------------
  130.  
  131. appitem.l = AddAppMenuItemA_(NUL,&user_data$,&amItem$,*wb_port,NUL) ;note use of &
  132.  
  133. ;-------------------------------------------------------------------
  134. ;Set up a commodity (serious stuff!)
  135. ;-------------------------------------------------------------------
  136.  
  137. *cx_port = CreateMsgPort_()     ;for communicating with Commodities Exchange
  138. cx_PortSig.l = 1 LSL *cx_port\mp_SigBit
  139.  
  140. ;initialise the NewBroker structure
  141. newbroker\nb_Version = #NB_VERSION,0,&cx_name$,&cx_title$,&cx_descr$,0,0,0,0,0,0
  142. newbroker\nb_Port = *cx_port        ;tell broker where its messageport will be
  143. *broker = CxBroker_(&newbroker, 0)  ;attach the broker to CX list
  144. ActivateCxObj_ *broker, 1           ;switch it on
  145.  
  146. ;-------------------------------------------------------------------
  147. ; MAIN PROGRAM LOOP
  148. ;-------------------------------------------------------------------
  149.  
  150. Repeat
  151.  
  152.   ;go to sleep and wait for messages from either port
  153.  
  154.   signal.l = Wait_(wb_PortSig | cx_PortSig)
  155.  
  156.   ;yawn, OK what's going on?
  157.  
  158.   ;try the commodity port...
  159.   cx_msg.l = GetMsg_(*cx_port)
  160.   If cx_msg
  161.  
  162.     Gosub proc_CX_commands
  163.     ReplyMsg_ cx_msg
  164.  
  165.   EndIf
  166.  
  167.   ;try the appmenuitem port...
  168.   *appmsg = GetMsg_(*wb_port)
  169.   If *appmsg AND enabled
  170.  
  171.     Gosub get_size
  172.     ReplyMsg_ *appmsg
  173.  
  174.   EndIf
  175.  
  176.  
  177. Until quit_prog = True
  178.  
  179. DeleteCxObj_(*broker)
  180. DeleteMsgPort_(*cx_port)
  181. RemoveAppMenuItem_(appitem)
  182. DeleteMsgPort_(*wb_port)
  183. FreeMem_ *fib, SizeOf.FileInfoBlock
  184.  
  185. End
  186.  
  187. ;**************************************************************
  188. ;  subroutines
  189. ;**************************************************************
  190.  
  191. ;-------------------------------------------------------------
  192. ;The Code!? ;(this is a very old comment, kept for posterity)
  193. ;-------------------------------------------------------------
  194.  
  195. get_size
  196.  
  197. *wbscreen = LockPubScreen_(&wbscreenname$)
  198. UnlockPubScreen_ NUL, *wbscreen
  199.  
  200. newdrawerflag = 0
  201. For icon_number = 0 To *appmsg\am_NumArgs-1 ;group select!
  202.  
  203.   Gosub GetDrawerPath ;get full path of drawer, returned in dpath$
  204.  
  205.   If good.l = 1   ;icon is a drawer
  206.     total = 0
  207.     GetSize{dpath$}
  208.     title$ = StripTrail$(dpath$,47) + "  " + StripLead$(Str$(total),32) + " Bytes"
  209.     ;        (remove trailing /)   (remove leading spaces added by Format statement)
  210.   Else
  211.  
  212.     ;good <> 1, icon is not a drawer
  213.     title$ = "Not a Drawer icon"
  214.     DisplayBeep_ 0
  215.  
  216.   EndIf
  217.   *wbscreen\Title = &title$
  218.   ShowTitle_ *wbscreen, -1
  219.  
  220.   ;reset default output if not a drawer icon
  221.   If good.l <> 1 Then title$ = "Select Drawer icon(s) for size in bytes."
  222.   newdrawerflag = 1
  223.  
  224. Next icon_number
  225.  
  226. ;choose message if no icon selected
  227. If newdrawerflag = 0
  228.  
  229.   If Left$(title$,6) = "Select"
  230.  
  231.     *wbscreen\Title = &title$
  232.     ShowTitle_ *wbscreen, -1
  233.  
  234.   Else
  235.  
  236.     output$ = "Last selected: " + title$
  237.  
  238.     *wbscreen\Title = &output$
  239.     ShowTitle_ *wbscreen, -1
  240.  
  241.   EndIf
  242.  
  243. EndIf
  244.  
  245. Return
  246.  
  247. ;---------------------------------------------------------------
  248.  proc_CX_commands ;Act on commands from Commodities Exchange
  249. ;---------------------------------------------------------------
  250.  
  251. msgid = CxMsgID_(cx_msg)  ;no need to get msgtype: no input events!!
  252.  
  253. Select msgid
  254.  
  255.   Case #CXCMD_DISABLE
  256.  
  257.     If enabled = True
  258.  
  259.       enabled = False
  260.       RemoveAppMenuItem_(appitem)
  261.       ActivateCxObj_ *broker, 0
  262.  
  263.     EndIf
  264.  
  265.   Case #CXCMD_ENABLE
  266.  
  267.     If enabled = False
  268.  
  269.       enabled = True
  270.       appitem = AddAppMenuItemA_(NUL,&user_data$,&amItem$,*wb_port,NUL)
  271.       ActivateCxObj_ *broker, 1
  272.  
  273.     EndIf
  274.  
  275.   Case #CXCMD_KILL
  276.  
  277.     enabled = False
  278.     quit_prog = True
  279.  
  280. End Select
  281.  
  282. Return
  283.  
  284. ;---------------------------------------------------------------
  285.  GetDrawerPath ;(by Aaron Koolen, BUM7)
  286. ;---------------------------------------------------------------
  287.  
  288.  
  289.   dpath$=""
  290.   args.l = *appmsg\am_ArgList + (icon_number * SizeOf.WBArg)
  291.   lock.l = Peek.l(args)               ; Lock of parent!
  292.   name$ = Peek$(Peek.l(args+4))       ; Name of icon
  293.  
  294. ; Get info about the file
  295.   good.l = Examine_(lock,*fib)
  296.  
  297.   If good<>0
  298.       dpath$=Peek$(&*fib\fib_FileName)
  299.  
  300. ; Make the entire pathname for the file
  301.     Repeat
  302.       lock=ParentDir_(lock)
  303.       If lock
  304.         If Examine_(lock,*fib)
  305.           dpath$=dpath$+"/"+name$
  306.           name$=dpath$
  307.           dpath$= Peek$(&*fib\fib_FileName)
  308.         EndIf
  309.       EndIf
  310.     Until lock=0
  311.  
  312.     dpath$=dpath$+":"+name$
  313.  
  314. ; Examine this name to check if it's a directory or not!
  315.     lock.l=Lock_(dpath$,-2)
  316.     If lock
  317.       If Examine_(lock,*fib)
  318.         If *fib\fib_DirEntryType>0
  319.           good = 1  :  dirLock.l = lock
  320.           d$=dpath$
  321.         EndIf
  322.       EndIf
  323.       UnLock_ lock
  324.       lock=0
  325.     EndIf
  326.   EndIf
  327. Return
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.